﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using eBay.Service.Core.Soap;
using eBay.Service.Core.Sdk;
using eBay.Service.Call;
using eBay.Service.Util;

namespace EbayMaster
{
   public partial class FrmMessage : Form
   {
      public ApiContext Context;

      public FrmMessage()
      {
         InitializeComponent();
      }

      private void FrmMessage_Load(object sender, EventArgs e)
      {
         DataTable dtMsg = EbayMessageDAL.GetAllMessagesTable();
         foreach (DataRow row in dtMsg.Rows)
         {
            String senderId = row["Sender"].ToString();
            String subject = row["Subject"].ToString();
            String ebayMsgId = row["EbayMessageId"].ToString();
            MessageInfoEntry entry = new MessageInfoEntry(senderId, subject, ebayMsgId);
            this.listBox1.Items.Add(entry);
         }

         WebBrowser browser = new WebBrowser();
         this.tabPageAllMessage.Controls.Add(browser);
         browser.Dock = DockStyle.Fill;
      }

      private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
      {
         MessageInfoEntry msgEntry = (MessageInfoEntry)this.listBox1.SelectedItem;
         if (msgEntry == null)
            return;

         String msgId = msgEntry.EbayMessageId;
         if (msgId == null)
            return;

         // Display msg in web brower.
         EbayMessageType msg = EbayMessageDAL.GetOneMessage(msgId);
         if (msg == null)
            return;

         WebBrowser browser = (WebBrowser)this.tabPageAllMessage.Controls[0];
         if (browser == null)
            return;

         browser.DocumentText = msg.Text;
      }

      public class MessageInfoEntry
      {
         public string SenderId;
         public string Subject;
         public string EbayMessageId;

         public MessageInfoEntry(string senderId, string subject, string ebayMsgId)
         {
            this.SenderId = senderId;
            this.Subject = subject;
            this.EbayMessageId = ebayMsgId;
         }

         public override string ToString()
         {
            return string.Format("{0} __ {1}", SenderId, Subject);
         }

      }  // class MessageInfoEntry

   }
}
